#include <iostream>
#include <string>
#include <queue>
using namespace std;

struct Node {
	string val;
	Node* left = nullptr;
	Node* right = nullptr;
};
string sp;
Node* H = nullptr;
void getTree(Node*& h)
{
	string cur;
	cin >> cur;
	if (cur == sp) return;
	h = new Node;
	h->val = cur;
	getTree(h->left);
	getTree(h->right);
}

int main()
{
	cin >> sp;
	getTree(H);
	
	queue<Node*> q;
	q.push(H);
	while (q.size()) 
	{
		auto cur = q.front(); q.pop();
		if (cur != H) cout << ",";
		cout << cur->val;
		if (cur->left) q.push(cur->left);
		if (cur->right) q.push(cur->right);
	}
	return 0;
}
